package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.entity.SpuSaleAttrValue;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.atguigu.gmall.product.vo.SpuInfoVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
    implements SpuInfoService{


    @Autowired
    SpuImageService spuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    @Autowired
    SpuSaleAttrValueService spuSaleAttrValueService;

    @Transactional
    @Override
    public void saveSpuInfoVo(SpuInfoVo spuInfoVo) {
        //1. 信息保存到spu_info
        SpuInfo spuInfo = new SpuInfo();
        BeanUtils.copyProperties(spuInfoVo,spuInfo);
        save(spuInfo);

        Long spuId = spuInfo.getId();

        //2. 保存到spu_image
        //2.1 拿到传过来得imageList
        List<SpuInfoVo.SpuImageListDTO> imageList = spuInfoVo.getSpuImageList();
        //2.2 建list来存储封装得images
        List<SpuImage> images = new ArrayList<>();
        for (SpuInfoVo.SpuImageListDTO spuImage:imageList){
            SpuImage image = new SpuImage();
            BeanUtils.copyProperties(spuImage,image);
            //回填spuId
            image.setSpuId(spuId);
            images.add(image);
        }
        spuImageService.saveBatch(images);

        //3. 保存到spu_sale_attr
        List<SpuInfoVo.SpuSaleAttrListDTO> saleAttrList = spuInfoVo.getSpuSaleAttrList();
        List<SpuSaleAttr> attrs = new ArrayList<>();
        List<SpuSaleAttrValue> values = new ArrayList<>();
        for (SpuInfoVo.SpuSaleAttrListDTO saleAttr : saleAttrList){
            SpuSaleAttr spuSaleAttr = new SpuSaleAttr();
            BeanUtils.copyProperties(saleAttr,spuSaleAttr);
            spuSaleAttr.setSpuId(spuId);
            attrs.add(spuSaleAttr);
            for (SpuInfoVo.SpuSaleAttrListDTO.SpuSaleAttrValueListDTO spuSaleAttrValueListDTO : saleAttr.getSpuSaleAttrValueList()){
                SpuSaleAttrValue spuSaleAttrValue = new SpuSaleAttrValue();
                BeanUtils.copyProperties(spuSaleAttrValueListDTO,spuSaleAttrValue);
                spuSaleAttrValue.setSpuId(spuId);
                spuSaleAttrValue.setSaleAttrName(saleAttr.getSaleAttrName());
                values.add(spuSaleAttrValue);
            }
        }
        spuSaleAttrService.saveBatch(attrs);

        //4. 保存到spu_sale_attr_value
        spuSaleAttrValueService.saveBatch(values);
    }
}




